package com.kust.smarthome.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kust.smarthome.web.dto.ShopDto;
import com.kust.smarthome.web.model.OrderForm;
import com.kust.smarthome.web.model.Shop;
import com.kust.smarthome.web.model.User;
import com.kust.smarthome.web.service.IOrderFormService;
import com.kust.smarthome.web.service.IShopService;
import com.kust.smarthome.web.service.IUserService;
import com.kust.smarthome.web.vo.ShopAddVo;
import com.kust.smarthome.web.vo.ShopBuyVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.Principal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author iAuzre
 * @since 2021-07-09
 */
@RestController
@RequestMapping("/shop")
@Api(value = "购物车相关", tags = {"shopping-tag"})
public class ShopController {

    @Resource
    private IShopService shopService;

    @Resource
    private IUserService userService;

    @Resource
    private IOrderFormService orderFormService;

    @GetMapping("")
    @ApiOperation(value = "获取购物车所有信息")
    public List<ShopDto> list(Principal principal) {
        User user = userService.getByUsername(principal.getName());

        return shopService.getShopList(user.getId());
    }

    @GetMapping("/add")
    @ApiOperation(value = "添加购物车", notes = "用户找到想要买的东西点击加入")
    public boolean addCommodity(Principal principal, ShopAddVo shopAddVo) {

        User user = userService.getByUsername(principal.getName());
        Shop shop = new Shop()
                .setCommodityId(shopAddVo.getCommodityId())
                .setUserId(user.getId())
                .setCommodityNumber(shopAddVo.getCommodityNumber());

        return shopService.save(shop);

    }

    @GetMapping("/deleteShopById")
    @ApiOperation(value = "删除对应购物车信息")
    public boolean deleteShopById(Principal principal, Integer id) {
        Shop shop = shopService.getById(id);
        User user = userService.getByUsername(principal.getName());

        if (shop.getUserId().equals(user.getId())) {
            return shopService.removeById(id);
        }

        return false;
    }

    @GetMapping("/addByNumber")
    @ApiOperation("添加数量")
    public boolean addByNumber(Principal principal, Integer shopId) {
        User user = userService.getByUsername(principal.getName());
        Shop shop = shopService.getById(shopId);
        shop.setCommodityNumber(shop.getCommodityNumber()+1);

        if (user.getId().equals(shop.getUserId())) {
            return shopService.updateById(shop);
        }
        return false;
    }

    @GetMapping("/reduceByNumber")
    @ApiOperation("减少数量")
    public boolean reduceByNumber(Principal principal, Integer shopId) {
        User user = userService.getByUsername(principal.getName());
        Shop shop = shopService.getById(shopId);
        shop.setCommodityNumber(shop.getCommodityNumber()-1);

        if (user.getId().equals(shop.getUserId())) {
            return shopService.updateById(shop);
        }
        return false;
    }

    @PostMapping("/buy")
    @ApiOperation("购物车购买")
    public String buy(Principal principal, ShopBuyVo shopBuyVo) {

        System.out.println(shopBuyVo.toString());
        User user = userService.getByUsername(principal.getName());

        String id = UUID.randomUUID().toString().replace("-","");

        String content = shopBuyVo.getContent();
        String[] commodityContent = content.split(";");
        String commodityIds = "", commodityNums = "";

        QueryWrapper<OrderForm> query = new QueryWrapper<>();
        query.orderByDesc("order_by").last("limit 1");
        OrderForm o = orderFormService.getOne(query);

        for (String c : commodityContent) {
            String[] cs = c.split("x");
            commodityIds = commodityIds + cs[0] + ";";
            commodityNums = commodityNums + cs[1] + ";";
        }

        commodityIds = commodityIds.substring(0, commodityIds.length()-1);
        commodityNums = commodityNums.substring(0, commodityNums.length()-1);


        OrderForm orderForm = new OrderForm()
                .setId(id)
                .setUserId(user.getId())
                .setCommodityId(commodityIds)
                .setCommodityNumber(commodityNums)
                .setMoney(shopBuyVo.getMoney())
                .setCreated(LocalDateTime.now())
                .setOrderBy(o.getOrderBy() + 1)
                .setState(0);

        if (orderFormService.save(orderForm))
            return id;

        return "";
    }

}
